﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TaiDouCommon.Model;

namespace TaiDouPhotonServer.DB.Manager
{
    //用户管理类
    public class UserManager
    {
        /// <summary>
        /// 查询用户名是否是否存在，若存在取第一组数据
        /// </summary>
        /// <param name="username"></param>
        /// <returns></returns>
        public User GetUserByUsername(string username)
        {
            using (var session = NHiberateHelper.OpenSession())
            {
                using (var transaction = session.BeginTransaction())
                {
                    var result = session.QueryOver<User>().Where(user=>user.UserName == username);
                    transaction.Commit();

                    if (result.List() != null && result.List().Count > 0)
                    {
                        return result.List()[0];
                    }
                    return null;
                }
            }
        }

        /// <summary>
        /// 注册新用户（注册前检查用户名是否已存在）
        /// </summary>
        /// <param name="user"></param>
        public void AddUser(User user)
        {
            using (var session = NHiberateHelper.OpenSession())
            {
                using (var transaction = session.BeginTransaction())
                {
                    session.Save(user);
                    transaction.Commit();
                }
            }
        }
    }
}
